package com.trg.generaltool.util.juc.pc;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: S2SHistoryParseNc
 * Description:
 * date: 2021-04-12 15:22
 *
 * @author tangrg
 * @since JDK 1.8
 */
public class S2SHistoryParseNc {
    private static final Logger log = LoggerFactory.getLogger(WorkDeal.class);

    public static final int THREAD_POOL_SIZE = 6;

    public void invok() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("pool-%d").build();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(6,
                THREAD_POOL_SIZE,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(6),
                namedThreadFactory,
                new ThreadPoolExecutor.DiscardOldestPolicy());
        try {
            WorkDeal workDeal = new WorkDeal();

            for (int i = 0; i < 5; i++) {
                executor.execute(() ->{
                    log.info("当前生产者对应线程："+Thread.currentThread().getName());
                    workDeal.producer();
                });

            }
            for (int i = 0; i < 5; i++) {

                executor.execute(() ->{
                    log.info("当前消费者对应线程："+Thread.currentThread().getName());
                    workDeal.customer();
                });
            }
            executor.shutdown();
            executor.awaitTermination(100L, TimeUnit.SECONDS);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executor.shutdown();
        }
    }

    public static void main(String[] args) {
        S2SHistoryParseNc s2SHistoryParseNc = new S2SHistoryParseNc();
        s2SHistoryParseNc.invok();
    }


}
